package com.wei.duowan.bbs.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.activity.BaseActivity;

import butterknife.ButterKnife;

/**
 * 作者：赵若位
 * 时间：2019/7/31 23:27
 * 联系方式：1070138445
 * 功能：
 */
public abstract class BaseFragment<T extends BasePresenter> extends Fragment
{
    public static final String TAG = BaseFragment.class.getSimpleName();

    protected abstract T createPresenter();

    protected abstract int getLayoutID();

    protected abstract void initView();

    protected T mPresenter;
    protected View mRootView;
    protected BaseActivity mActivity;


    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        this.mActivity = (BaseActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        if (mRootView == null)
        {
            mRootView = inflater.inflate(getLayoutID(), container, false);
            ButterKnife.bind(this, mRootView);
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    /*在Include中设置标题*/
    protected void setTitle(@Nullable String msg)
    {
        if (mRootView == null || TextUtils.isEmpty(msg) || mRootView.findViewById(R.id.tv_title) == null)
        {
            return;
        }
        TextView tv_title = mRootView.findViewById(R.id.tv_title);
        tv_title.setText(msg);
    }

    /*在Include中设置标题*/
    public void setTitle(@StringRes int resourceId)
    {
        this.setTitle(App.getResource().getString(resourceId));
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
